/**
 * Created by
 */
define(['jquery','layer'], function ($,Layer) {
    /**
     * 拼接表单 json
     * @param keyValue
     * @param ignore
     * @returns {*}
     * @constructor
     */
    var GetWebControls = function(keyValue,ignore) {
        var reVal = "";
        $("#form").find("input,select,textarea,.form-control").not(ignore).each(function (r) {
            var id = $(this).attr("id");
            var type = $(this).attr("type");
            switch (type) {
                case "checkbox":
                    if ($("#" + id).is(":checked")) {
                        reVal += "\"" + id + "\"" + ":" + "\"1\",";
                    } else {
                        reVal += "\"" + id + "\"" + ":" + "\"0\",";
                    }
                    break;
                case "select":
                    if($("#" + id).prop("disabled")){
                        break;
                    }
                    var value = $("#" + id).val();
                    if (value == "") {
                        value = "&nbsp;";
                    }

                    if($("#" + id).attr("name")=="resourceTarget"){
                        reVal += "\"" + "resourceTarget" + "\"" + ":" + "\"" + $.trim(value) + "\",";
                        break;
                    }

                    reVal += "\"" + id + "\"" + ":" + "\"" + $.trim(value) + "\",";
                    break;
                case "selectTree":
                    var value = $("#" + id).attr("data-value");
                    if (value == "") {
                        value = "&nbsp;";
                    }
                    reVal += "\"" + id + "\"" + ":" + "\"" + $.trim(value) + "\",";
                    break;
                default:
                    var value = $("#" + id).val();
                    if (value == "") {
                        value = "&nbsp;";
                    }
                    reVal += "\"" + id + "\"" + ":" + "\"" + $.trim(value) + "\",";
                    break;
            }
        });
        reVal = reVal.substr(0, reVal.length - 1);

        if (!keyValue) {
            reVal = reVal.replace(/&nbsp;/g, "");
        }
        reVal = reVal.replace(/\\/g, "\\\\");
        reVal = reVal.replace(/\n/g, "\\n");
        var postdata = jQuery.parseJSON("{" + reVal + "}");
        return postdata;
    };

    /**
     * 自动填充表单
     * @param data
     * @constructor
     */
    var SetWebControls = function (data) {

        var $id = $("#form");
        for (var key in data) {
            var id = $id.find("#" + key);
            if (id.attr("id")) {
                var type = id.attr("type");

                if (id.hasClass("input-datepicker")) {
                    type = "datepicker";
                }
                if (!type && id[0] && (id[0].nodeName === "DIV" || id[0].nodeName === "SPAN")) {
                    type = id[0].nodeName;
                }
                var value = $.trim(data[key]).replace(/&nbsp;/g, "");

                switch (type) {
                    case "checkbox":
                        break;
                    case "select":
                        id.val(value).trigger("change");

                        break;
                    case "selectTree":
                        id.val(value).trigger("change");
                        break;
                    case "datepicker":
                        //id.val(formatDate(value, "yyyy-MM-dd"));
                        break;
                    case "DIV":
                    case "SPAN":
                        id.html(value);
                        break;
                    default:
                        id.val(value);
                        break;
                }

            }
        }
        var status = $.trim(data["status"]).replace(/&nbsp;/g, "");
        if (data[status] == 1||status == true) {
            $("#status").bootstrapSwitch('toggleState');
        }
    }
    return{
        GetWebControls:GetWebControls,
        SetWebControls:SetWebControls
    }
});